//
//  YMProductView.swift
//  ProductViewDemo
//
//  Created by youmy on 2018/11/27.
//  Copyright © 2018 youmy. All rights reserved.
//

import UIKit

public class YMProductView: UIView {

    @IBOutlet weak var productImageV: UIImageView!
    @IBOutlet weak var productNameLb: UILabel!
    @IBOutlet weak var rentLb: UILabel!
    @IBOutlet weak var signValueLb: UILabel!
    @IBOutlet weak var specsV: UIView!
    
    public var originY:CGFloat = 0 {
        didSet{
            setNeedsLayout()
        }
    }
    
    public override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()
        width = UIScreen.main.bounds.width
        y = originY
    }
    
    public func valueConfig(model:GZAZJOrderDetailModel?){
        
        productNameLb.text = model?.materielModelName
        productImageV.downloadedFrom(imageurl: model?.thumbnail ?? "")
        rentLb.text = String.init(format: "租金：￥%.2f/月", (model?.leaseAmount ?? 0).roundTo(places: 2))
        signValueLb.text = String.init(format: "签约价值：￥%.2f", (model?.signContractAmount ?? 0).roundTo(places: 2))
        
        let values = model?.newMaterielSpecName.split(separator: Character(","))
        var i = 0
        var nextX:CGFloat = 0
        for value in values ?? []{
            let label = UILabel()
            label.backgroundColor = UIColor(valueRGB: 0xf6f8fa, alpha: 1)
            label.textColor = UIColor(valueRGB: 0x242424, alpha: 1)
            label.font = UIFont.init(name: PingFangSC_Regular, size: 11)
            label.text = String(value)
            label.textAlignment = .center
            label.layer.cornerRadius = 3
            label.layer.masksToBounds = true
            label.sizeToFit()
            label.width = label.width + 10
            label.height = 24
            label.x = nextX + (i != 0 ? 5 : 0)
            label.y = 0
            nextX = label.frame.maxX
            specsV.addSubview(label)
            i = i + 1
        }
    }
    
    public class func nibView() -> YMProductView {
        let bundle = Bundle(for: YMProductView.classForCoder())
        return bundle.loadNibNamed("YMProductView", owner: nil, options: nil)?.last as! YMProductView
    }
}
